﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MealBooking.Core;
using MealBooking.Core.Models.Accounts;

namespace ThisIsFirstTest.Areas.Manager.Controllers
{
    public class AccountManagerController : Controller
    {
        public ActionResult Index()
        {
            var accounts = CoreService.AccountService.AllAccounts();
            return View(accounts);
        }
        public ActionResult Detail(string id, string headerMessage = null)
        {
            var account = CoreService.AccountService.AccountDetail(new Account { Id = id });
            ViewBag.Account = account;
            var customer = CoreService.AccountService.CustomerDetail(id);
            ViewBag.Customer = customer;
            if (headerMessage != null)
            {
                ViewBag.HeaderMessage = headerMessage;
            }
            return View();
        }
        public ActionResult EditAccount(string id)
        {
            var account = CoreService.AccountService.AccountDetail(new Account { Id = id });
            return View(account);
        }
        [HttpPost]
        public ActionResult EditAccount(Account newAccount)
        {
            var account = CoreService.AccountService.AccountDetail(newAccount);
            account.IsActived = newAccount.IsActived;
            account.Comment = newAccount.Comment;
            account = CoreService.AccountService.UpdateAccount(account);
            return RedirectToAction("Detail", new { id = account.Id, headerMessage = "Update Success. " });
        }
        public ActionResult EditCustomer(string id)
        {
            var customer = CoreService.AccountService.CustomerDetail(id);
            ViewBag.Customer = customer;
            return View();
        }
        [HttpPost]
        public ActionResult EditCustomer(BalanceHistory balanceHistory)
        {
            if (!ModelState.IsValid)
            {
                Customer customer = CoreService.AccountService.CustomerDetail(balanceHistory.Account.Id);
                ViewBag.Customer = customer;
                return View(balanceHistory);
            }
            Customer account = CoreService.AccountService.CustomerDetail(balanceHistory.Account.Id);
            balanceHistory.Account = new Account { Id = account.Id, Name = account.Name };
            var manager = CoreService.AccountService.ManagerDetail(User.Identity.Name);
            balanceHistory.OperatedBy = new Account { Id = manager.Id, Name = manager.Name };
            balanceHistory.OperationType = BalanceOperationType.AddBalance;
            CoreService.AccountService.AddBalance(balanceHistory);
            return RedirectToAction("Detail", new { id = account.Id, headerMessage = "Update Success. " });
        }
        #region Balance
        public ActionResult BalanceHistory(string id)
        {
            var customer = CoreService.AccountService.CustomerDetail(id);
            ViewBag.Customer = customer;
            var balanceHistories = CoreService.AccountService.BalanceHistory(id);
            return View(balanceHistories);
        }
        #endregion
    }
}
